﻿using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Xml.Linq;
using UnityEngine;
using UnityEngine.UI;
public class RequirementViewData
{
    public int minAge;
    public int maxAge;
    public int minHeight;
    public int maxHeight;
    public int minEdu;
    public bool sameLocation;
    public bool sameHometown;
    public bool onlyUnmarried;
    public RequirementViewData(Requirement require)
    {
        this.minAge = require.minAge;
        this.maxAge = require.maxAge;
        this.minHeight = require.minHeight;
        this.maxHeight = require.maxHeight;
        this.minEdu = require.minEdu;
        this.sameLocation = require.sameLocation;
        this.sameHometown = require.sameHometown;
        this.onlyUnmarried = require.onlyUnmarried;
    }
    public static Requirement RequirementViewData2Data(RequirementViewData viewData)
    {
        var requireData = new Requirement();
        requireData.minAge = viewData.minAge;
        requireData.maxAge = viewData.maxAge;
        requireData.minHeight = viewData.minHeight;
        requireData.maxHeight = viewData.maxHeight;
        requireData.minEdu = viewData.minEdu;
        requireData.sameLocation = viewData.sameLocation;
        requireData.sameHometown = viewData.sameHometown;
        requireData.onlyUnmarried = viewData.onlyUnmarried;
        return requireData;
    }
    public static bool Compare(Requirement requireData, RequirementViewData viewData)
    {
        return requireData.minAge == viewData.minAge &&
        requireData.maxAge == viewData.maxAge &&
        requireData.minHeight == viewData.minHeight &&
        requireData.maxHeight == viewData.maxHeight &&
        requireData.minEdu == viewData.minEdu &&
        requireData.sameLocation == viewData.sameLocation &&
        requireData.sameHometown == viewData.sameHometown &&
        requireData.onlyUnmarried == viewData.onlyUnmarried;
    }
}
public class EditRequirementPanel : AppUI
{
    public DoubleSideProgress ageProgress;
    public Text ageRangeText;
    public DoubleSideProgress heightProgress;
    public Text heightRangeText;
    public ListTogglesGroup eduGroup;
    public ListTogglesGroup locationGroup;
    public ListTogglesGroup hometownGroup;
    public ListTogglesGroup maritalStatusGroup;

    RequirementViewData m_Data;

    // Start is called before the first frame update
    public override void Start()
    {

    }


    public void Init(Requirement requirement)
    {
        ageProgress.onLeftValueUpdate = OnAgeRangeUpdate;
        ageProgress.onRightValueUpdate = OnAgeRangeUpdate;

        heightProgress.onLeftValueUpdate = OnHeightRangeUpdate;
        heightProgress.onRightValueUpdate = OnHeightRangeUpdate;

        m_Data = new RequirementViewData(requirement);

        ageProgress.Init(requirement.minAge, requirement.maxAge);
        heightProgress.Init(requirement.minHeight, requirement.maxHeight);

        GameDebug.Log($"minAge:{requirement.minAge}  maxAge:{requirement.maxAge}");

        eduGroup.Init(OnSelectEdu, requirement.minEdu);
        locationGroup.Init(OnSelectLocation, requirement.sameLocation?1:0);
        hometownGroup.Init(OnSelectHometown, requirement.sameHometown?1:0);
        maritalStatusGroup.Init(OnSelectMaritalStatus, requirement.onlyUnmarried?1:0);
    }

    private void OnAgeRangeUpdate(int obj)
    {
        ageRangeText.text = $"{ageProgress.curLeftValue} - {ageProgress.curRightValue}";
        m_Data.minAge = ageProgress.curLeftValue;
        m_Data.maxAge = ageProgress.curRightValue;
    }

    private void OnHeightRangeUpdate(int obj)
    {
        heightRangeText.text = $"{heightProgress.curLeftValue} - {heightProgress.curRightValue}";
        m_Data.minHeight = heightProgress.curLeftValue;
        m_Data.maxHeight = heightProgress.curRightValue;
    }
    private void OnSelectEdu(int minEdu)
    {
        m_Data.minEdu = minEdu;
    }
    private void OnSelectMaritalStatus(int value)
    {
        m_Data.onlyUnmarried = value > 0;
    }

    private void OnSelectHometown(int value)
    {
        m_Data.sameHometown = value > 0;
    }

    private void OnSelectLocation(int value)
    {
        m_Data.sameLocation = value > 0;
    }


    // Update is called once per frame
    void Update()
    {
        
    }


    public void GoBack()
    {
        if (RequirementViewData.Compare(MGame.instance.MUser.requirement, m_Data))
        {
            UIController.ShowUI(UIController.prePanel.GetType());
        }
        else
        {
            GameDebug.Log($"更新数据 minAge:{m_Data.minAge}  maxAge:{m_Data.maxAge}");

            PanelLoading.Show(5);

            var requireData = RequirementViewData.RequirementViewData2Data(m_Data);
            RequestServerController.instance.ReqUpdateRequirement(requireData, (requirement) =>
            {
                PanelLoading.Hide();
                UIController.ShowUI(UIController.prePanel.GetType());
            });
        }
    }

}
